class AuditsController < ApplicationController
  
  before_filter :require_user
  
  # GET /audits
  # GET /audits.json
  def index
    #@audits = Audit.all
    authorize! :read, Audit
    @search = Audit.search(params[:search])
    @search.meta_sort ||= 'created_at.desc'
    @audits = @search.paginate(:per_page => 10 ,:page => params[:page])
    
    @models = Model.find(:all)
    @model_list = Array.new
    @models.each do |model|
      @model_list << model.to_combo
    end
    
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @audits }
    end
  end

  # GET /audits/1
  # GET /audits/1.json
  def show
    authorize! :read, Audit
    @audit = Audit.find(params[:id])
    @read_only = true
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @audit }
    end
  end

  # GET /audits/new
  # GET /audits/new.json
  def new
    authorize! :create, Audit
    @audit = Audit.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @audit }
    end
  end

  # GET /audits/1/edit
  def edit
    authorize! :update , Audit
    @audit = Audit.find(params[:id])
  end

  # POST /audits
  # POST /audits.json
  def create
    authorize! :create , Audit
    @audit = Audit.new(params[:audit])
    respond_to do |format|
      if @audit.save
        format.html { redirect_to @audit, notice: 'Audit was successfully created.' }
        format.json { render json: @audit, status: :created, location: @audit }
      else
        format.html { render action: "new" }
        format.json { render json: @audit.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /audits/1
  # PUT /audits/1.json
  def update
    authorize! :update , Audit
    @audit = Audit.find(params[:id])
    respond_to do |format|
      if @audit.update_attributes(params[:audit])
        format.html { redirect_to @audit, notice: 'Audit was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @audit.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /audits/1
  # DELETE /audits/1.json
  def destroy
    authorize! :destroy , Audit
    @audit = Audit.find(params[:id])
    @audit.destroy

    respond_to do |format|
      format.html { redirect_to audits_url }
      format.json { head :ok }
    end
  end
end
